package com.hd.crdao.activity;

import android.content.Intent;
import android.os.Handler;
import android.view.View;
import android.widget.AdapterView;

import com.google.gson.Gson;
import com.hd.crdao.R;
import com.hd.crdao.adapter.MessageAdapter;
import com.hd.crdao.base.BaseSecondActivity;
import com.hd.crdao.bean.MessageBean;
import com.hd.crdao.config.Config;
import com.hd.crdao.config.Constants;
import com.hd.crdao.utils.MyToast;
import com.hd.crdao.utils.SPUtils;
import com.hd.crdao.utils.UIUtils;
import com.hd.crdao.view.ExpandHeadView;
import com.hd.crdao.view.MyListView;
import com.zhy.http.okhttp.OkHttpUtils;
import com.zhy.http.okhttp.callback.StringCallback;

import java.util.List;

import butterknife.BindView;
import okhttp3.Call;

/**
 *
 *@author Programming
 *@date 2017/5/22 0022
 */
public class MessageCenterActivity extends BaseSecondActivity {
    @BindView(R.id.messagecenter)
    ExpandHeadView mMessagecenter;
    @BindView(R.id.messagelist)
    MyListView mMessagelist;
    public List<MessageBean.RowsBean> mMessageBeanRows;
    public MessageAdapter mMessageAdapter;
    public Handler mHandler = new Handler();

    @Override
    protected void initView() {
        initData();

        mMessagelist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                MessageBean.RowsBean rowsBean = mMessageBeanRows.get(position-1);
                Intent intent = new Intent(UIUtils.getContext(), MessageDetailActivity.class);
                intent.putExtra("messagedetail", rowsBean);
                startActivity(intent);
            }
        });
        mMessagelist.setonRefreshListener(new MyListView.OnRefreshListener() {
            @Override
            public void onRefresh() {
                mHandler.postDelayed(new TaskRunnable(), 1500);
            }
        });
    }


    @Override
    protected int setContentView() {
        return R.layout.activity_messagecenter;
    }

    @Override
    protected ExpandHeadView setExpandHeadView() {
        return mMessagecenter;
    }

    private void initData() {

        String userid = SPUtils.getString(Config.userId, "");
        OkHttpUtils.post().url(Constants.MESSAGE_URL)
                .addParams("data", userid)
                .addParams("sign", Constants.SIGN)
                .build().execute(new StringCallback() {
            @Override
            public void onError(Call call, Exception e, int id) {
                MyToast.show("服务器连接异常");
            }

            @Override
            public void onResponse(String response, int id) {
                MessageBean messageBean = new Gson().fromJson(response, MessageBean.class);
                mMessageBeanRows = messageBean.getRows();
                mMessageAdapter = new MessageAdapter(mMessageBeanRows);
                mMessagelist.setAdapter(mMessageAdapter);
            }
        });
    }


    private class TaskRunnable implements Runnable {
        @Override
        public void run() {
            initData();
            mMessageAdapter.notifyDataSetChanged();
            mMessagelist.onRefreshComplete();
        }
    }
}
